
package com.posplus.losspreventiondataimporter.svt;

import com.posplus.lossprevention.dao.DepartmentsDAO;
import com.posplus.lossprevention.mapping.Departments;
import com.posplus.lossprevention.mapping.Products;
import com.posplus.strack.mapping.Product;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;

/**
 *
 * @author ScottW
 */
public class ProductsMapper {
    public static List<Products> svtToLp(List<Product> svtProducts, DepartmentsDAO lpDepartmentsDAO){
        List<Products> lpProducts = new ArrayList<Products>();
        List<Departments> lpDepartments = lpDepartmentsDAO.findAll();
        Map<Integer, Departments> departmentMap = listToMap(lpDepartments);
        
        
        Iterator<Product> iter = svtProducts.iterator();
        while(iter.hasNext()){
            Product svtProduct = iter.next();
            try{
                Departments department = departmentMap.get(svtProduct.getDepartment().intValue());
                if(department == null)
                    throw new RuntimeException("Department number: " + svtProduct.getDepartment() + " was not found");
                Products lpProduct = new Products(department, svtProduct.getUpc(), svtProduct.getDescription().trim(), null);
                lpProducts.add(lpProduct);
            }catch(RuntimeException ex){
                Logger.getLogger(ProductsMapper.class).warn(ex.getMessage());
            }
        }

        return lpProducts;
    }
    
    public static Map<Integer, Departments> listToMap(List<Departments> list){
        Map<Integer, Departments> result = new HashMap<Integer, Departments>();
        
        Iterator<Departments> iter = list.iterator();
        while(iter.hasNext()){
            Departments next = iter.next();
            result.put(next.getNumber(), next);
        }
        
        return result;
    }
}